package com.cat.util;

import com.cat.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author :HYC  email: kobe524348@gmail.com
 * @program : HYC_CatRecruit
 * @name : ServletUtils
 * @description : servlet tools
 * @create : 2019-03-28 00:09
 */
public class ServletUtils {

    private ServletUtils() {
    }

    /**
     * @return com.cat.pojo.Student
     * @name Mapping2Stu
     * @description 用于将用户提交的表单信息映射成一个学生对象
     * @notice 请注意对于表单中为空的信息，会映射为“无”
     * @author HYC kobe524348@gmail.com
     * @date 2019/4/2
     */
    public static Student Mapping2Stu(HttpServletRequest req) {
        Student stu = new Student();

        stu = BeanUtil.StudentMapper(
                req.getParameter("studentId")
                , req.getParameter("studentName")
                , req.getParameter("phoneNumber")
                //对密码进行md5加密
                , MD5Util.getDigest(req.getParameter("password"))
                , req.getParameter("userType")
                , req.getParameter("portrait")
                , req.getParameter("gender")
                , req.getParameter("major")
                , req.getParameter("majorClass")
                , req.getParameter("position")
                , req.getParameter("introduction"));
        return stu;
    }


    /**
     * @param title 填写错误信息或者提示信息的标题
     * @param p     填写信息的正文，通常是向用户提供解决方案
     * @name showError
     * @description 用于向用户输出错误信息或者提示信息
     * @notice 请注意标题的长度不应该过长，并且不要向此页面输出系统具体的错误信息，不能在此页面打印堆栈信息，只能输出转换后的错误内容
     * @author HYC kobe524348@gmail.com
     * @date 2019/4/2
     */
    public static void showError(HttpServletRequest req, HttpServletResponse res, String title, String p) {
        req.setAttribute("message.title", title);
        req.setAttribute("message.p", p);
        try {
            req.getRequestDispatcher("error.jsp").forward(req, res);
        } catch (ServletException | IOException e) {
            //TODO
            e.printStackTrace();
        }
    }
}